package tech.aistar.prj.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/user/loginView")
public class LoginViewController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //TODO... 接受重定向过来的error参数
        //如果url地址上没有这个error参数,那么error是null值.
        String error = req.getParameter("error");

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html lang='en'>");
        out.println("<head>");
        out.println("<meta charset='UTF-8'>");
        out.println("<meta http-equiv='X-UA-Compatible' content='IE=edge'>");
        out.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
        out.println("<title>bootstrap是一款响应式的UI[User-Interface]框架</title>");
        out.println("<!-- 引入bootstrap.css - 开发环境/bootstrap.min.css - 生产环境-->");
        out.println("<link rel='stylesheet' href='/servlet/plugins/bootstrap-3.4.1-dist/css/bootstrap.css'>");
        out.println("<link rel='stylesheet' href='/servlet/css/user/login.css'>");
        out.println("</head>");
        out.println("<body>");

        out.println("<div class='container'>");
        out.println("<div class='row login'>");
        //TODO... action
        out.println("<form id='formLogin' class='form-horizontal' role='form' action='/servlet/user/login' method='post'> ");
        out.println("<fieldset>");
        out.println("<legend>");out.println("<h3>用户登录入口");
        out.println("</h3>");out.println("</legend>");

        out.println("<div class='form-group'>");
        out.println("<label for='firstname' class='col-sm-2 control-label'>用户名");out.println("</label>");
        out.println("<div class='col-md-6'>");
        //TODO... 根据控件的name属性的值...
        //TODO... 控件的name属性的值尽量和实体类的属性名保持一致
//        String uName = "";
//        String pwd = "";
//        Cookie[] cookies = req.getCookies();
//        if (null!=cookies && cookies.length>0){
//            for (Cookie c : cookies) {
//
//                uName = c.getName();
//                pwd = c.getValue();
//            }
//        }
        out.println("       <input type='text' name='username' class='form-control'" +
                " id='username' placeholder='请输入用户名''>");

        if("0".equals(error)) {
            //TODO... 判断是否显示用户不存在的
            out.println("   <label class='label label-danger'>* sorry,用户不存在!</label>");
        }

        out.println("</div>");
        out.println("</div>");
        out.println("<div class='form-group'>");
        out.println("<label for='lastname' class='col-sm-2 control-label'>密码");out.println("</label>");
        out.println("<div class='col-md-6'>");
        //TODO...
        out.println("<input type='password' class='form-control' " +
                " name='password' id='password' placeholder='请输入密码' >");
        if("1".equals(error)) {
            //TODO... 判断是否显示密码不存在
            out.println("   <label class='label label-danger'>* sorry,密码错误!</label>");
        }
        out.println("</div>");
        out.println("</div>");
        out.println("<div class='form-group'>");
        out.println("<div class='col-sm-offset-2 col-sm-10'>");
        out.println("<div class='checkbox'>");
        out.println("<label>");
        out.println("<input type='checkbox' name='check' id='ck'>请记住我");
        out.println("</label>");
        out.println("</div>");
        out.println("</div>");
        out.println("</div>");
        out.println("<div class='form-group'>");
        out.println("<div class='col-sm-offset-2 col-sm-10'>");
        //TODO... submit按钮
        out.println("<button type='button' id='login_btn' class='btn btn-default'>登录");out.println("</button>");
        out.println("</div>");
        out.println("</div>");
        out.println("</fieldset>");
        out.println("</form>");
        out.println("</div>");
        out.println("</div>");


        out.println("<script src='/servlet/plugins/jquery.min.js'>");out.println("</script>");
        out.println("<script src='/servlet/plugins/bootstrap-3.4.1-dist/js/bootstrap.js'>");out.println("</script>");
        out.println("<script src='/servlet/js/user/login.js'>");out.println("</script>");
        out.println("</body>");
        out.println("</html>");

    }
}
